<template>
    <!-- 对话框表单 -->
    <!-- 建议使用 Prettier 格式化代码 -->
    <!-- el-form 内可以混用 el-form-item、FormItem、ba-input 等输入组件 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
        width="50%"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="config.layout.shrink ? '':'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @submit.prevent=""
                    :model="baTable.form.items"
                    :label-position="config.layout.shrink ? 'top' : 'right'"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                >
                    <FormItem :label="t('supplier.company_name')" type="string" v-model="baTable.form.items!.company_name" prop="company_name" :placeholder="t('Please input field', { field: t('supplier.company_name') })" />
                    <FormItem :label="t('supplier.tel')" type="string" v-model="baTable.form.items!.tel" prop="tel" :placeholder="t('Please input field', { field: t('supplier.tel') })" />
                    <FormItem :label="t('supplier.account_type')" type="select" v-model="baTable.form.items!.account_type" prop="account_type" :data="{ content: { '1': t('supplier.account_type 1'), '2': t('supplier.account_type 2') } }" :placeholder="t('Please select field', { field: t('supplier.account_type') })" />
                    <template v-if="baTable.form.items!.account_type == 1">
                        <FormItem :label="t('supplier.account_name')" type="string" v-model="baTable.form.items!.account_name" prop="account_name" :placeholder="t('Please input field', { field: t('supplier.account_name') })" />
                    </template>
                    <FormItem :label="t('supplier.account_no')" type="string" v-model="baTable.form.items!.account_no" prop="account_no" :placeholder="t('Please input field', { field: t('supplier.account_no') })" />
                    <FormItem :label="t('supplier.account_bank')" type="string" v-model="baTable.form.items!.account_bank" prop="account_bank" :placeholder="t('Please input field', { field: t('supplier.account_bank') })" />
                    <FormItem :label="t('supplier.account_bank_name')" type="string" v-model="baTable.form.items!.account_bank_name" prop="account_bank_name" :placeholder="t('Please input field', { field: t('supplier.account_bank_name') })" />
                    <FormItem :label="t('supplier.account_contact_name')" type="string" v-model="baTable.form.items!.account_contact_name" prop="account_contact_name" :placeholder="t('Please input field', { field: t('supplier.account_contact_name') })" />
                    <template v-if="baTable.form.items!.account_type == 2">
                        <FormItem :label="t('supplier.person_name')" type="string" v-model="baTable.form.items!.person_name" prop="person_name" :placeholder="t('Please input field', { field: t('supplier.person_name') })" />
                        <FormItem :label="t('supplier.person_idcard')" type="string" v-model="baTable.form.items!.person_idcard" prop="person_idcard" :placeholder="t('Please input field', { field: t('supplier.person_idcard') })" />
                    </template>
                    <FormItem :label="t('supplier.person_idcard_file')" type="image" v-model="baTable.form.items!.person_idcard_file" prop="person_idcard_file" />
                    <FormItem :label="t('supplier.contact_tel')" type="string" v-model="baTable.form.items!.contact_tel" prop="contact_tel" :placeholder="t('Please input field', { field: t('supplier.contact_tel') })" />
                    <FormItem label="项目名称" type="textarea" v-model="baTable.form.items!.project_titles" :inputAttr="{rows: 5}"  :placeholder="t('Please input field', { field: t('项目名称') })" />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm()">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import type { FormInstance, FormItemRule } from 'element-plus'
import { inject, reactive, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import FormItem from '/@/components/formItem/index.vue'
import { useConfig } from '/@/stores/config'
import type baTableClass from '/@/utils/baTable'
import { buildValidatorData } from '/@/utils/validate'

const config = useConfig()
const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    company_name: [buildValidatorData({ name: 'required', title: t('supplier.company_name') })],
    tel: [buildValidatorData({ name: 'required', title: t('公司联系电话') })],
    account_name: [buildValidatorData({ name: 'required', title: t('supplier.account_name') })],
    account_no: [buildValidatorData({ name: 'required', title: t('supplier.account_no') })],
    account_bank: [buildValidatorData({ name: 'required', title: t('supplier.account_bank') })],
    account_bank_name: [buildValidatorData({ name: 'required', title: t('supplier.account_bank_name') })],
    account_contact_name: [buildValidatorData({ name: 'required', title: t('supplier.account_contact_name') })],
    person_name: [buildValidatorData({ name: 'required', title: t('supplier.person_name') })],
    person_idcard: [
        buildValidatorData({ name: 'required', title: t('supplier.person_idcard') }),
        buildValidatorData({ name: 'idNumber', title: '请输入正确的身份证号' })
    ],
    person_idcard_file: [buildValidatorData({ name: 'required', title: t('supplier.person_idcard_file') })],
    contact_tel: [buildValidatorData({ name: 'required', title: t('supplier.contact_tel') })],
    
})
</script>

<style scoped lang="scss"></style>
